//
//  MainNavigationVC.m
//  TuanLife
//
//  Created by WuJianMac on 2017/12/22.
//  Copyright © 2017年 HuiNaMac. All rights reserved.
//

#import "MainNavigationVC.h"

@interface MainNavigationVC ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation MainNavigationVC

- (id)init
{
    self = [super initWithNavigationBarClass:[UINavigationBar class] toolbarClass:nil];
    if(self) {
        
    }
    return self;
}

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
    self = [super initWithNavigationBarClass:[UINavigationBar class] toolbarClass:nil];
    if(self) {
        self.viewControllers = @[rootViewController];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationStatus];
    //解决设置左按钮引起的 手势侧滑失效
    self.interactivePopGestureRecognizer.delegate = self;
    self.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

#pragma mark --delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.viewControllers.count <= 1 || self.visibleViewController == [self.viewControllers objectAtIndex:0] || [self.visibleViewController isKindOfClass:NSClassFromString(@"XCShopPayListViewController")]) {
        return NO;
    }
    return YES;
}

//deal with tabbar overlap
//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    for (UIView *tabBar in self.tabBarController.tabBar.subviews) {
//        if ([tabBar isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//            [tabBar removeFromSuperview];
//        }
//    }
//}

- (void)setNavigationStatus
{
    self.navigationBar.translucent = NO;
    self.delegate = self;
    self.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationBar.tintColor = [UIColor whiteColor];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
     
    UIFont* font = [UIFont fontWithName:@"PingFangSC-Medium" size: 16.0];
    if (font) {
        self.navigationBar.titleTextAttributes = @{
                                                   NSForegroundColorAttributeName: [UIColor blackColor],
                                                   NSFontAttributeName:font
                                                   };
    }else{
        self.navigationBar.titleTextAttributes = @{
                                                   NSForegroundColorAttributeName: [UIColor blackColor],
                                                   };
    }
    
    //NavigationBar Background color
    self.navigationBar.barTintColor = XCMainColor;
    
    //Navigationcontroller Back's picture
    UIImage *buttonNormal = [[UIImage imageNamed:@"返回（黑）"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationBar setBackIndicatorImage:buttonNormal];
    [self.navigationBar setBackIndicatorTransitionMaskImage:buttonNormal];
    
    // Remove left UIBarButtonItem's title, set this value to offscreen
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-30, 0)
//                                                         forBarMetrics:UIBarMetricsDefault];
//    //Navigationcontroller shadow line
//    self.navigationBar.shadowImage = [[UIImage alloc] init];
    
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
        if (font) {
            appearance.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor],
                                                       NSFontAttributeName:font };
        }else{
            appearance.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor],
                                           };
        }
        [appearance setBackgroundColor: XCMainColor];
        // 隐藏分割线 设置一个透明或者纯色的图片 设置nil 或者 [UIImage new]无效
        [appearance setBackgroundImage:[UIImage zt_imageWithPureColor:XCMainColor]];
//        [appearance setShadowImage:[UIImage zt_imageWithPureColor:XCMainColor]];
        [appearance setShadowImage:[UIImage zt_imageWithPureColor:[UIColor clearColor]]];
//        [appearance setShadowImage:[[UIImage alloc] init]];
        self.navigationBar.scrollEdgeAppearance = appearance;
        self.navigationBar.standardAppearance =appearance;
    }
}


- (void)setNavigationClear
{
//        self.navigationBar.translucent = YES;
//        self.navigationBar.shadowImage = [UIImage new];
//        [self.navigationBar setBackgroundImage:[self imageWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationBarHeight) alphe:0 andColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//        UIFont* font = [UIFont fontWithName:@"PingFangSC-Medium" size: 16.0];
    UIColor *color = [UIColor colorNamed:@"5676fc"];
//    [self.navigationBar setBackgroundImage:[self imageWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationBarHeight) alphe:0 andColor:color] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setBackgroundColor:color];
    UIFont* font = [UIFont fontWithName:@"PingFangSC-Medium" size: 16.0];
        if (font) {
            self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:font};
        }else{
            self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],};
        }
    
    
//    if (@available(iOS 15.0, *)) {
//        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
//        //        appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
//        [appearance setShadowImage:[[UIImage alloc] init]];
//        if (font) {
//            appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:font};
//        }else{
//            appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],};
//        }
//        [appearance setBackgroundColor: [UIColor colorNamed:@"fDarkBuleColor"]];
//        // 隐藏分割线 设置一个透明或者纯色的图片 设置nil 或者 [UIImage new]无效
//        [appearance setBackgroundImage:[self imageWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationBarHeight) alphe:0 andColor:[UIColor clearColor]]];
////        [appearance setShadowImage:[UIImage zt_imageWithPureColor:[UIColor colorNamed:@"fDarkOrgColor"]]];
//        [[UINavigationBar appearance] setScrollEdgeAppearance: appearance];
//    }
}

- (UIImage *)imageWithFrame:(CGRect)frame alphe:(CGFloat)alphe andColor:(UIColor*)color
{
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[color colorWithAlphaComponent:alphe] CGColor]);
    CGContextFillRect(context, frame);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
        
    }
    [super pushViewController:viewController animated:animated];
}

-(UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

-(UIViewController *)childViewControllerForStatusBarHidden {
    return self.topViewController;
}


- (BOOL)shouldAutorotate {
    return self.topViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.topViewController.supportedInterfaceOrientations;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return self.topViewController.preferredInterfaceOrientationForPresentation;
}
@end

//
//+(void)load
//{
//    UINavigationBar *Bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
//    //设置导航条标题字体
//    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
//    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:16];
//    Bar.titleTextAttributes = attr;
////        Bar.backgroundColor = [UIColor                         redColor];
////    Bar.translucent = YES;
//
//}
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//}
//
//
//@end
//
